import pygame
from pytmx import load_pygame, TiledObjectGroup

from Actor.Chinese_soldier import ChineseFireSoldier
from Actor.Lead_actor import Lead_Actor
from Actor.PrizesSprite import PrizesSprite
from Actor.japanese_soldier import JapaneseWalkSoldier
from Scene.BaseScene import BaseScene, ScenePassStatus
from Scene.Task.BaseTask import TaskStatus
from Scene.Task.Task1 import TakePrizesTask


class TunnelWarScene(BaseScene):
    """
    游戏画面场景
    """
    def __init__(self, yu_er):
        super(TunnelWarScene, self).__init__(yu_er,
                                             "../Resources/tmx/任务1场景.png")

        self.japanese_group = pygame.sprite.Group()
        self.prizes_group = pygame.sprite.Group()
        self.obstacle_group = pygame.sprite.Group()
        self.tiled_map_data = load_pygame("../Resources/tmx/map2.tmx")
        self.init_character()
        self.font = pygame.font.Font("../Resources/font/迷你简粗宋.TTF", 15)
        self.tasks = []
        self.tasks.append(TakePrizesTask(self.yu_er, self.prizes_group, self.font))
        self.task_id = 0
        pygame.mixer.music.load("../Resources/sound/十送红军.mp3")
        pygame.mixer.music.play()

    def draw_actor(self):
        for obj in self.japanese_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)
            x, y, w, h = obj.rect
            pygame.draw.rect(self.current_surface, (255, 0, 0),
                             (x - self.view_x, y - self.view_y, w, h), 2)

        for obj in self.prizes_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)

        for obj in self.obstacle_group:
            x, y, w, h = obj.rect
            pygame.draw.rect(self.current_surface, (255, 255, 0),
                             (x - self.view_x, y - self.view_y, w, h), 2)

        self.yu_er.draw(self.current_surface, self.view_x, self.view_y)
        self.captain.draw(self.current_surface, self.view_x, self.view_y)
        self.vice_captain.draw(self.current_surface, self.view_x, self.view_y)

    def init_character(self):
        """
        初始化场景中的人物
        """
        self.captain = None
        self.vice_captain = None
        for group in self.tiled_map_data.objectgroups:
            if isinstance(group, TiledObjectGroup):
                if group.name == "敌人":
                    for obj in group:
                        japanese = JapaneseWalkSoldier(obj.x, obj.y)
                        self.japanese_group.add(japanese)
                elif group.name == "物品":
                    for obj in group:
                        prizes = PrizesSprite(obj.x, obj.y)
                        prizes.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.prizes_group.add(prizes)
                        print(obj.x, obj.y, obj.width, obj.height)
                elif group.name == "障碍物":
                    for obj in group:
                        obstacle = pygame.sprite.Sprite()
                        obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obstacle)
                elif group.name == "主角":
                    for obj in group:
                        if obj.name == "队长":
                            self.captain = ChineseFireSoldier(obj.x, obj.y)
                        if obj.name == "副队长":
                            self.vice_captain = ChineseFireSoldier(obj.x, obj.y)
                        if obj.name == "鱼儿":
                            self.yu_er = Lead_Actor(obj.x, obj.y)

    def run(self, key_down_flag, key_list):
        self.yu_er.run(key_down_flag, key_list, self.obstacle_group, self.japanese_group)
        for japanese in self.japanese_group:
            japanese.run(key_down_flag, key_list, self.obstacle_group)

        if key_down_flag and key_list[pygame.K_1]:
            self.pass_status = ScenePassStatus.final_win

        if key_down_flag and key_list[pygame.K_2]:
            self.pass_status = ScenePassStatus.final_fail

        self.tasks[self.task_id].do_task(key_down_flag, key_list)

        if self.tasks[self.task_id].get_state() == TaskStatus.win_over:
            self.task_id += 1
            if self.task_id >= len(self.tasks):
                self.pass_status = ScenePassStatus.final_win
                pygame.mixer.music.stop()
        elif self.tasks[self.task_id].get_state() == TaskStatus.fail_over:
            self.pass_status = ScenePassStatus.final_fail
            pygame.mixer.music.stop()

    def get_pass_status(self):
        if self.pass_status == ScenePassStatus.final_win:
            return self.pass_status, "WinScene"
        elif self.pass_status == ScenePassStatus.final_fail:
            return self.pass_status, "Fail_Scene"
        else:
            return self.pass_status, None
